﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using DataUpload;

namespace logistics
{
    public partial class SetSytem : Form
    {
        PrintDocument bqPrint = new PrintDocument();
        UpdatePwd pwd = null;
        clearCustom clu = null;
        string localFile = Application.StartupPath + "\\wumaotong.xml";
        public SetSytem()
        {
            InitializeComponent();
            pnlSelectPrint.Visible = true;
            pnltagesPrinitPage.Visible = false;
            pnlwaybill.Visible = false;
            pnlConsignor.Visible = false;

            IntializeCombox();
            string _ticketPrint = XmlFilex.Read(localFile, "/system/print/ticket", "");
            lbltickPrint.Text = string.IsNullOrEmpty(_ticketPrint) ? "暂未设置" : _ticketPrint;
            string path = "/system/print/Tags";
            txtmaxnum.Text = XmlFilex.Read(localFile, path, "maxNum");
            SetCol();
        }

        private void SetCol()
        {
            string path = "/system/col/Consignor";
            string col = XmlFilex.Read(localFile, path, "");
            if (!string.IsNullOrEmpty(col))
            {
                char[] tag = col.ToCharArray();
                if (tag[0] == '1')
                {
                    ckbuser.Checked = true;
                }
                else
                {
                    ckbuser.Checked = false;
                }
                if (tag[1] == '1')
                {
                    ckbUserTel.Checked = true;
                }
                else
                {
                    ckbUserTel.Checked = false;
                }
                if (tag[2] == '1')
                {
                    ckbUserMobile.Checked = true;
                }
                else
                {
                    ckbUserMobile.Checked = false;
                }
                if (tag[3] == '1')
                {
                    ckbUserBank.Checked = true;
                }
                else
                {
                    ckbUserBank.Checked = false;
                }
            }
        }

        private void IntializeCombox()
        {
            PrintDocument print = new PrintDocument();
            string sDefault = print.PrinterSettings.PrinterName;//默认打印机名           
            lbldefault.Text = "系统目前默认打印机(即标签打印机)：";
            lblPrintDefault.Text = sDefault;

            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                if (sPrint != sDefault)
                {
                    cmbwaybill.Items.Add(sPrint);
                }
            }
            bqPrint.PrintPage += new PrintPageEventHandler(bqPrint_PrintPage);

        }

        void bqPrint_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            DrawWaybill(g);//打运单
        }

        private void btnUpdatePwd_Click(object sender, EventArgs e)
        {
            if (pwd == null)
            {
                pwd = new UpdatePwd();
                pwd.Show();
            }
            else
            {
                if (pwd.IsDisposed)
                {
                    pwd = new UpdatePwd();
                    pwd.Show();
                }
                else
                {
                    pwd.Activate();
                }
            }
        }

        private void btnselectPrints_Click(object sender, EventArgs e)
        {
            pnlSelectPrint.Visible = true;
            pnltagesPrinitPage.Visible = false;
            pnlwaybill.Visible = false;
            pnlTagScore.Visible = false;
            pnlConsignor.Visible = false;
        }

        private void btnSetTagePrint_Click(object sender, EventArgs e)
        {
            pnlSelectPrint.Visible = false;
            pnltagesPrinitPage.Visible = true;
            pnlwaybill.Visible = false;
            pnlTagScore.Visible = false;
            pnlConsignor.Visible = false;
        }

        private void btnWaybillPrint_Click(object sender, EventArgs e)
        {
            int X = int.Parse(XmlFilex.Read(localFile, "/system/print/ticket", "X"));
            int Y = int.Parse(XmlFilex.Read(localFile, "/system/print/ticket", "Y"));
            int _fontsize = int.Parse(XmlFilex.Read(localFile, "/system/print/ticket", "fontsize"));
            bool bold = bool.Parse(XmlFilex.Read(localFile, "/system/print/ticket", "Bold"));
            txtX.Text = X.ToString();
            txtY.Text = Y.ToString();
            fontsize.Value = _fontsize;
            pnlSelectPrint.Visible = false;
            pnltagesPrinitPage.Visible = false;
            pnlwaybill.Visible = true;
            pnlConsignor.Visible = false;
            if (bold)
            {
                chbBold.Checked = true;
            }
            else
            {
                chbBold.Checked = false;
            }
        }

        private void btnwaybillSave_Click(object sender, EventArgs e)
        {
            string x = txtX.Text;
            string y = txtY.Text;
            decimal _fontSize = fontsize.Value;
            string blod = chbBold.Checked.ToString();
            if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y))
            {
                MessageBox.Show("顶点坐标不能为空");
                return;
            }
            decimal mleft = waybillLeftMove.Value;
            decimal mRight = waybillRightMove.Value;
            decimal mUP = waybillUpMove.Value;
            decimal mDown = waybilldownMove.Value;

            decimal LR = mRight - mleft;
            decimal UD = mDown - mUP;

            try
            {
                string path = "/system/print/ticket";
                XmlFilex.Update(localFile, path, "X", x);
                XmlFilex.Update(localFile, path, "Y", y);
                XmlFilex.Update(localFile, path, "left", LR.ToString());
                XmlFilex.Update(localFile, path, "top", UD.ToString());
                XmlFilex.Update(localFile, path, "fontsize", _fontSize.ToString());
                XmlFilex.Update(localFile, path, "Bold", blod);

                MessageBox.Show("设置成功");
            }
            catch (Exception)
            {
                MessageBox.Show("设置失败");
            }
        }

        /// <summary>
        /// 保存打印机选择设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnselectPrintSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cmbwaybill.Text))
            {
                MessageBox.Show("请选择小票打印机");
                return;
            }

            string tickePrint = cmbwaybill.SelectedItem.ToString();
            try
            {
                XmlFilex.Update(localFile, "/system/print/Tags", "", lblPrintDefault.Text);
                XmlFilex.Update(localFile, "/system/print/ticket", "", tickePrint);
                lbltickPrint.Text = tickePrint;
                MessageBox.Show("设置保存成功！");
            }
            catch (Exception)
            {
                MessageBox.Show("保存失败");
            }
        }


        private void btntagesMoveSave_Click(object sender, EventArgs e)
        {

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            DataHelp dh = new DataHelp();
            string _ticketPrint = XmlFilex.Read(Application.StartupPath + "\\wumaotong.xml", "/system/print/ticket", "");
            dh.SetDefaultPrinter(_ticketPrint);
            string x = txtX.Text;
            string y = txtY.Text;
            if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y))
            {
                MessageBox.Show("顶点坐标不能为空");
                return;
            }
            bqPrint.Print();
        }


        private void DrawWaybill(Graphics g)
        {
            int X = int.Parse(XmlFilex.Read(Application.StartupPath + "\\wumaotong.xml", "/system/print/ticket", "X"));
            int Y = int.Parse(XmlFilex.Read(Application.StartupPath + "\\wumaotong.xml", "/system/print/ticket", "Y"));
            int moveX = int.Parse(XmlFilex.Read(Application.StartupPath + "\\wumaotong.xml", "/system/print/ticket", "left"));
            int moveY = int.Parse(XmlFilex.Read(Application.StartupPath + "\\wumaotong.xml", "/system/print/ticket", "top"));
            int topx = X + moveX;
            int topy = Y + moveY;

            Font font = new Font("宋体", 12);
            Brush bru = Brushes.Black;
            //头行
            g.DrawString("打印时间", font, bru, topx + 80, topy - 10);
            g.DrawString("运单号", font, bru, topx + 250, topy - 10);
            g.DrawString("货号", font, bru, topx + 400, topy - 10);

            //1行
            g.DrawString("XXX1", font, bru, topx + 50, topy + 25);
            g.DrawString("XXX1", font, bru, topx + 200, topy + 25);
            g.DrawString("XXX1", font, bru, topx + 350, topy + 25);

            //2行
            g.DrawString("XXX2", font, bru, topx + 50, topy + 50);
            g.DrawString("XXX2", font, bru, topx + 200, topy + 50);
            g.DrawString("XXX2", font, bru, topx + 350, topy + 50);

            //3行
            g.DrawString("XXX31", font, bru, topx + 20, topy + 100);
            g.DrawString("XXX32", font, bru, topx + 120, topy + 100);
            g.DrawString("XXX33", font, bru, topx + 220, topy + 100);
            g.DrawString("XXX34", font, bru, topx + 270, topy + 100);
            g.DrawString("XXX35", font, bru, topx + 330, topy + 100);
            g.DrawString("XXX36", font, bru, topx + 420, topy + 100);

            //4行
            g.DrawString("XXX41", font, bru, topx + 20, topy + 150);
            g.DrawString("XXX42", font, bru, topx + 120, topy + 150);
            g.DrawString("XXX43", font, bru, topx + 250, topy + 150);
            g.DrawString("XXX44", font, bru, topx + 420, topy + 150);

            //5行
            g.DrawString("XXX5", font, bru, topx + 60, topy + 180);
            g.DrawString("XXX5A", font, bru, topx + 420, topy + 180);
            g.DrawString("XXX5B", font, bru, topx + 420, topy + 210);
            g.DrawString("XXX5C", font, bru, topx + 420, topy + 235);


            //6行
            g.DrawString("XXX6", font, bru, topx + 60, topy + 260);
            g.DrawString("XXX6", font, bru, topx + 220, topy + 260);
            g.DrawString("XXX6", font, bru, topx + 420, topy + 260);

        }

        private void txtX_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() != "\b" && !(char.IsDigit(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void txtmaxnum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() != "\b" && !(char.IsDigit(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void btnNumSave_Click(object sender, EventArgs e)
        {
            string maxNum = txtmaxnum.Text.Trim();
            if (string.IsNullOrEmpty(maxNum))
            {
                MessageBox.Show("打印份数不能为空");
            }
            else
            {
                string path = "/system/print/Tags";
                XmlFilex.Update(localFile, path, "maxNum", maxNum);
                MessageBox.Show("数据保存成功");
            }

        }
        //标签高级设置
        private void buttonItem1_Click(object sender, EventArgs e)
        {
            pnlSelectPrint.Visible = false;
            pnltagesPrinitPage.Visible = false;
            pnlwaybill.Visible = false;
            pnlTagScore.Visible = true;
            pnlConsignor.Visible = false;
            string path = "/system/print/Tags";
            txtmaxnum.Text = XmlFilex.Read(localFile, path, "maxNum");
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            pnlConsignor.Visible = true;
            pnlSelectPrint.Visible = false;
            pnltagesPrinitPage.Visible = false;
            pnlwaybill.Visible = false;
            pnlTagScore.Visible = false;
        }

        private void btncollectionSave_Click(object sender, EventArgs e)
        {
            string user = ckbuser.Checked ? "1" : " 0";
            string tel = ckbUserTel.Checked ? "1" : " 0";
            string mobile = ckbUserMobile.Checked ? "1" : " 0";
            string bank = ckbUserBank.Checked ? "1" : " 0";
            string result = user.Trim() + tel.Trim() + mobile.Trim() + bank.Trim();
            string path = "/system/col/Consignor";
            XmlFilex.Update(localFile, path, "", result);
            MessageBox.Show("数据保存成功");
        }

        private void btnClrearCustom_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("请确定客户信息处理吗？这样将花费很长时间。", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK)
            {
                pnlConsignor.Visible = false;
                pnlSelectPrint.Visible = false;
                pnltagesPrinitPage.Visible = false;
                pnlwaybill.Visible = false;
                pnlTagScore.Visible = false;

                if (clu == null)
                {
                    clu = new clearCustom();
                    clu.Show();
                   // this.Close();
                    this.btnClrearCustom.Enabled = false;
                }
                else
                {
                    if (clu.IsDisposed)
                    {
                        clu = new clearCustom();
                        clu.Show();
                       // this.Close();
                        this.btnClrearCustom.Enabled = false;
                    }
                    else
                    {
                        clu.Activate();
                    }
                }
            }

        }

        private void btnRemoteSite_Click(object sender, EventArgs e)
        {

        }
    }
}
